<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/6/2 15:33
 */

namespace App\Transformers;

use App\Models\City;
use App\Models\Patient;
use App\User;
use League\Fractal\TransformerAbstract;

class PatientEditTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['records', 'address'];

    /**
     * @var \App\Project\FieldTranslation\FieldTranslation
     */
    protected $field;

    public function __construct()
    {
        $this->field = app('field')->get('patient');
    }

    /**
     * @param \App\Models\Patient $patient
     * @return array
     */
    public function transform(Patient $patient)
    {
        //  病历信息
        $transform = [
            'id'              => $patient->id,
            'name'            => $patient->name,
            'sex'             => $patient->sex,
            'phone'           => $patient->phone,
            'birthday'        => $patient->birthday,
            'medical_number'  => $patient->medical_number,
            'diabetes_type'   => $patient->diabetes_type,
            'special_illness' => $patient->special_illness,
            'cardnumber'      => $patient->CARDNUMBER,
            'nation'          => $patient->nation,
            'province'        => $patient->province,
            'city'            => $patient->city,
            'county'          => $patient->county,
            'diabetes_stage'  => $patient->diabetes_stage,
        ];

        $record = $patient->medicalRecords()->getQuery()->first(['clinic_at', 'hospital_id']);
        //        dd($record);

        if (!is_null($record)) {
            $transform['first_visit'] = $record->clinic_at;
        } else {
            $transform['first_visit'] = '';
        }

        if (!is_null($patient->hospital_id) && $patient->hospital_id > 0) {
            $transform['hospital'] = [
                'key'   => $patient->hospital_id,
                'value' => $patient->hospital()->getQuery()->first(['name'])->name
            ];
        } elseif (!is_null($record) && $record->hospital_id > 0) {
            $transform['hospital'] = [
                'key'   => $record->hospital_id,
                'value' => $record->hospital()->getQuery()->first(['name'])->name
            ];
        }

        if ($doctor_id = $patient->patientsDoctor()) {

            $doctor = User::withTrashed()->find($doctor_id, ['name']);

            $transform['doctor'] = [
                'key'   => $doctor_id,
                'value' => $doctor ? $doctor->name : ''
            ];
        }

        if (!is_null($patient->created_at)) {
            $transform['created_at'] = $patient->created_at->toDateTimeString();
        }

        if (!is_null($patient->updated_at)) {
            $transform['updated_at'] = $patient->updated_at->toDateTimeString();
        }

        return $transform;
    }

    /**
     * 看诊信息记录
     * @param \App\Models\Patient $patient
     * @return \League\Fractal\Resource\Collection
     */
    public function includeRecords(Patient $patient)
    {
        return $this->collection($patient->medicalRecords, new MedicalRecordTransformer());
    }

    public function includeAddress(Patient $patient)
    {
        return $this->item($patient, function ($patient) {

            $data = [];

            if ($patient->province) {
                $data['province'] = City::select('name')->find($patient->province)->name;
            }

            if ($patient->city) {
                $data['city'] = City::select('name')->find($patient->city)->name;
            }

            if ($patient->county) {
                $data['county'] = City::select('name')->find($patient->county)->name;
            }

            return $data;
        });
    }
}